package com.ruoyi.project.lesson.media.controller;

import com.ruoyi.common.utils.MediaUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.media.domain.LessonMedia;
import com.ruoyi.project.lesson.media.service.ILessonMediaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 音视频管理Controller
 *
 * @author shixiangwen
 * @date 2020-12-11
 */
@Controller
@RequestMapping("/lesson/media")
public class LessonMediaController extends BaseController {
    private String prefix = "lesson/media";

    @Autowired
    private ILessonMediaService lessonMediaService;

    @RequiresPermissions("lesson:media:view")
    @GetMapping()
    public String media() {
        return prefix + "/media";
    }

    /**
     * 查询音视频管理列表
     */
    @RequiresPermissions("lesson:media:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonMedia lessonMedia) {
        startPage();
        List<LessonMedia> list = lessonMediaService.selectLessonMediaList(lessonMedia);
        return getDataTable(list);
    }

    /**
     * 导出音视频管理列表
     */
    @RequiresPermissions("lesson:media:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonMedia lessonMedia) {
        List<LessonMedia> list = lessonMediaService.selectLessonMediaList(lessonMedia);
        ExcelUtil<LessonMedia> util = new ExcelUtil<LessonMedia>(LessonMedia.class);
        return util.exportExcel(list, "media");
    }

    /**
     * 新增音视频管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存音视频管理
     */
    @RequiresPermissions("lesson:media:add")
    @Log(title = "音视频管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonMedia lessonMedia) {
        return toAjax(lessonMediaService.insertLessonMedia(lessonMedia));
    }

    /**
     * 修改音视频管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonMedia lessonMedia = lessonMediaService.selectLessonMediaById(id);
        mmap.put("lessonMedia", lessonMedia);
        return prefix + "/edit";
    }

    @GetMapping("/play/{vodId}")
    public String play(@PathVariable("vodId") String vodId, ModelMap mmap) {
        String playUrl = MediaUtil.getPlayUrl(vodId);
        mmap.put("playUrl", playUrl);
        LessonMedia lessonMedia = lessonMediaService.selectLessonMediaByVodId(vodId);
        if (lessonMedia != null) {
            mmap.put("width", lessonMedia.getWidth());
            mmap.put("height", lessonMedia.getHeight());
        }
        return prefix + "/play";
    }

    /**
     * 修改保存音视频管理
     */
    @RequiresPermissions("lesson:media:edit")
    @Log(title = "音视频管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonMedia lessonMedia) {
        return toAjax(lessonMediaService.updateLessonMedia(lessonMedia));
    }

    /**
     * 删除音视频管理
     */
    @RequiresPermissions("lesson:media:remove")
    @Log(title = "音视频管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonMediaService.deleteLessonMediaByIds(ids));
    }
}
